/************************************************
 *** Datei: DroneSwarmUI.cpp
 ***
 *** Erstellt am: 03.09.2012
 ***       Autor: Max Stark <Max.Stark88@googlemail.com>
 ***	 Version: 1.0
 ***
 *** Beschreibung:
 *** <hier Beschreibung einfuegen>
 ************************************************/

#include "DroneSwarmUI.h"

DroneSwarmUI::DroneSwarmUI(UIController* uiController) {
	this->uiController = uiController;
	this->uiController->attachUIObserver(this);

	this->listSize = 0;
}

void DroneSwarmUI::update(int update) {
	std::system("cls");

	cout << "*** Drohnen im Netz ***" << endl;
	vector<DroneListEntry>* droneList = this->uiController->getDroneList();
	for(unsigned int i=0; i<droneList->size(); ++i) {
		DroneListEntry drone = droneList->at(i);

		HANDLE screen;
		screen = GetStdHandle(STD_OUTPUT_HANDLE);

		// Schriftfarbe WHITE
		//SetConsoleTextAttribute(screen, 15);

		if(drone.masterDrone) {
			// Schriftfarbe BLUE
			SetConsoleTextAttribute(screen, 9);
		} else if(drone.slaveDrone) {
			// Schriftfarbe GREEN
			SetConsoleTextAttribute(screen, 2);
		}

		cout << "("<< i+1 << ") " << drone.strDroneHostName << ", "
				<< drone.strDroneModelName << ", "
				<< drone.strIPAddress << endl;

		// Schriftfarbe WHITE
		SetConsoleTextAttribute(screen, 7);
	}
	this->listSize = droneList->size();

	cout << endl << "***  Eigene Drohne  ***" << endl;
	IDroneNavData* navData = this->uiController->getDroneNavData();
	cout << "Akku:            " << navData->getBattery() << endl;
	cout << "Hoehe:           " << navData->getHeight() << endl;
	cout << "Geschwindigkeit: " << navData->getVelocity() << endl;

	cout << "***********************"<< endl;
}

void DroneSwarmUI::run() {
	bool running = true;

	this->update(0);
	string eingabe;

	while(running) {
		cin >> eingabe;

		if(eingabe=="1" && this->listSize>=1) {
			this->uiController->sendMasterMessage(0);

		} else if(eingabe=="2" && this->listSize>=2) {
			this->uiController->sendMasterMessage(1);

		} else if(eingabe=="3" && this->listSize>=3) {
			this->uiController->sendMasterMessage(2);

		} else if(eingabe=="4" && this->listSize>=4) {
			this->uiController->sendMasterMessage(3);

		} else if(eingabe=="5" && this->listSize>=5) {
			this->uiController->sendMasterMessage(4);

		} else if(eingabe=="c") {
			cout << "connect..." << endl;
			this->uiController->connectDrone();

		} else if(eingabe=="q") {
			cout << "beenden..." << endl;
			this->uiController->disconnectDrone();
			running = false;
		}

	}

}

DroneSwarmUI::~DroneSwarmUI() {
}

